home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / iscan.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  29.4 KB  |  1,169 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: iscan.c,v 1.5 2000/09/19 19:00:46 lpd Exp $ */
  20. /* Token scanner for Ghostscript interpreter */
  21. #include "ghost.h"
  22. #include "memory_.h"
  23. #include "stream.h"
  24. #include "errors.h"
  25. #include "btoken.h"        /* for ref_binary_object_format */
  26. #include "files.h"        /* for fptr */
  27. #include "ialloc.h"
  28. #include "idict.h"        /* for //name lookup */
  29. #include "dstack.h"        /* ditto */
  30. #include "ilevel.h"
  31. #include "iname.h"
  32. #include "ipacked.h"
  33. #include "iparray.h"
  34. #include "strimpl.h"        /* for string decoding */
  35. #include "sa85d.h"        /* ditto */
  36. #include "sfilter.h"        /* ditto */
  37. #include "ostack.h"        /* for accumulating proc bodies; */
  38.                     /* must precede iscan.h */
  39. #include "iscan.h"        /* defines interface */
  40. #include "iscanbin.h"
  41. #include "iscannum.h"
  42. #include "istream.h"
  43. #include "istruct.h"        /* for RELOC_REF_VAR */
  44. #include "iutil.h"
  45. #include "ivmspace.h"
  46. #include "store.h"
  47. #include "scanchar.h"
  48.  
  49. #define recognize_btokens()\
  50.   (ref_binary_object_format.value.intval != 0 && level2_enabled)
  51.  
  52. #ifdef DEBUG
  53. /* Dummy comment processing procedure for testing. */
  54. private int
  55. no_comment_proc(const byte * str, uint len)
  56. {
  57.     return 0;
  58. }
  59. #endif
  60.  
  61. /* Procedure for handling DSC comments if desired. */
  62. /* Set at initialization if a DSC handling module is included. */
  63. int (*scan_dsc_proc) (P2(const byte *, uint)) = NULL;
  64.  
  65. /* Procedure for handling all comments if desired. */
  66. /* Set at initialization if a comment handling module is included. */
  67. /* If both scan_comment_proc and scan_dsc_proc are set, */
  68. /* scan_comment_proc is called only for non-DSC comments. */
  69. int (*scan_comment_proc) (P2(const byte *, uint)) = NULL;
  70.  
  71. /*
  72.  * Level 2 includes some changes in the scanner:
  73.  *      - \ is always recognized in strings, regardless of the data source;
  74.  *      - << and >> are legal tokens;
  75.  *      - <~ introduces an ASCII85 encoded string (terminated by ~>);
  76.  *      - Character codes above 127 introduce binary objects.
  77.  * We explicitly enable or disable these changes here.
  78.  */
  79. #define scan_enable_level2 level2_enabled    /* from ilevel.h */
  80.  
  81. /* ------ Dynamic strings ------ */
  82.  
  83. /* Begin collecting a dynamically allocated string. */
  84. inline private void
  85. dynamic_init(da_ptr pda, gs_memory_t *mem)
  86. {
  87.     pda->is_dynamic = false;
  88.     pda->limit = pda->buf + da_buf_size;
  89.     pda->next = pda->base = pda->buf;
  90.     pda->memory = mem;
  91. }
  92.  
  93. /* Free a dynamic string. */
  94. private void
  95. dynamic_free(da_ptr pda)
  96. {
  97.     if (pda->is_dynamic)
  98.     gs_free_string(pda->memory, pda->base, da_size(pda), "scanner");
  99. }
  100.  
  101. /* Resize a dynamic string. */
  102. /* If the allocation fails, return e_VMerror; otherwise, return 0. */
  103. private int
  104. dynamic_resize(da_ptr pda, uint new_size)
  105. {
  106.     uint old_size = da_size(pda);
  107.     uint pos = pda->next - pda->base;
  108.     gs_memory_t *mem = pda->memory;
  109.     byte *base;
  110.  
  111.     if (pda->is_dynamic) {
  112.     base = gs_resize_string(mem, pda->base, old_size,
  113.                 new_size, "scanner");
  114.     if (base == 0)
  115.         return_error(e_VMerror);
  116.     } else {            /* switching from static to dynamic */
  117.     base = gs_alloc_string(mem, new_size, "scanner");
  118.     if (base == 0)
  119.         return_error(e_VMerror);
  120.     memcpy(base, pda->base, min(old_size, new_size));
  121.     pda->is_dynamic = true;
  122.     }
  123.     pda->base = base;
  124.     pda->next = base + pos;
  125.     pda->limit = base + new_size;
  126.     return 0;
  127. }
  128.  
  129. /* Grow a dynamic string. */
  130. /* Return 0 if the allocation failed, the new 'next' ptr if OK. */
  131. /* Return 0 or an error code, updating pda->next to point to the first */
  132. /* available byte after growing. */
  133. private int
  134. dynamic_grow(da_ptr pda, byte * next, uint max_size)
  135. {
  136.     uint old_size = da_size(pda);
  137.     uint new_size = (old_size < 10 ? 20 :
  138.              old_size >= (max_size >> 1) ? max_size :
  139.              old_size << 1);
  140.     int code;
  141.  
  142.     pda->next = next;
  143.     if (old_size == max_size)
  144.     return_error(e_limitcheck);
  145.     while ((code = dynamic_resize(pda, new_size)) < 0 &&
  146.        new_size > old_size
  147.     ) {            /* Try trimming down the requested new size. */
  148.     new_size -= (new_size - old_size + 1) >> 1;
  149.     }
  150.     return code;
  151. }
  152.  
  153. /* Ensure that a dynamic string is either on the heap or in the */
  154. /* private buffer. */
  155. private void
  156. dynamic_save(da_ptr pda)
  157. {
  158.     if (!pda->is_dynamic && pda->base != pda->buf) {
  159.     memcpy(pda->buf, pda->base, da_size(pda));
  160.     pda->next = pda->buf + da_size(pda);
  161.     pda->base = pda->buf;
  162.     }
  163. }
  164.  
  165. /* Finish collecting a dynamic string. */
  166. private int
  167. dynamic_make_string(i_ctx_t *i_ctx_p, ref * pref, da_ptr pda, byte * next)
  168. {
  169.     uint size = (pda->next = next) - pda->base;
  170.     int code = dynamic_resize(pda, size);
  171.  
  172.     if (code < 0)
  173.     return code;
  174.     make_tasv_new(pref, t_string,
  175.           a_all | imemory_space((gs_ref_memory_t *) pda->memory),
  176.           size, bytes, pda->base);
  177.     return 0;
  178. }
  179.  
  180. /* ------ Main scanner ------ */
  181.  
  182. /* GC procedures */
  183. #define ssarray ssptr->s_ss.binary.bin_array
  184. private 
  185. CLEAR_MARKS_PROC(scanner_clear_marks)
  186. {
  187.     scanner_state *const ssptr = vptr;
  188.  
  189.     r_clear_attrs(&ssarray, l_mark);
  190. }
  191. private 
  192. ENUM_PTRS_WITH(scanner_enum_ptrs, scanner_state *ssptr) return 0;
  193. case 0:
  194.     if (ssptr->s_scan_type == scanning_none ||
  195.     !ssptr->s_da.is_dynamic
  196.     )
  197.     ENUM_RETURN(0);
  198.     return ENUM_STRING2(ssptr->s_da.base, da_size(&ssptr->s_da));
  199. case 1:
  200.     if (ssptr->s_scan_type != scanning_binary)
  201.     return 0;
  202.     ENUM_RETURN_REF(&ssarray);
  203. ENUM_PTRS_END
  204. private RELOC_PTRS_WITH(scanner_reloc_ptrs, scanner_state *ssptr)
  205. {
  206.     if (ssptr->s_scan_type != scanning_none && ssptr->s_da.is_dynamic) {
  207.     gs_string sda;
  208.  
  209.     sda.data = ssptr->s_da.base;
  210.     sda.size = da_size(&ssptr->s_da);
  211.     RELOC_STRING_VAR(sda);
  212.     ssptr->s_da.limit = sda.data + sda.size;
  213.     ssptr->s_da.next = sda.data + (ssptr->s_da.next - ssptr->s_da.base);
  214.     ssptr->s_da.base = sda.data;
  215.     }
  216.     if (ssptr->s_scan_type == scanning_binary) {
  217.     RELOC_REF_VAR(ssarray);
  218.     r_clear_attrs(&ssarray, l_mark);
  219.     }
  220. }
  221. RELOC_PTRS_END
  222. /* Structure type */
  223. public_st_scanner_state();
  224.  
  225. /* Initialize a scanner. */
  226. void
  227. scanner_state_init_options(scanner_state *sstate, int options)
  228. {
  229.     sstate->s_scan_type = scanning_none;
  230.     sstate->s_pstack = 0;
  231.     sstate->s_options = options;
  232. }
  233.  
  234. /* Handle a scan_Refill return from scan_token. */
  235. /* This may return o_push_estack, 0 (meaning just call scan_token again), */
  236. /* or an error code. */
  237. int
  238. scan_handle_refill(i_ctx_t *i_ctx_p, const ref * fop, scanner_state * sstate,
  239.            bool save, bool push_file, op_proc_t cont)
  240. {
  241.     stream *s = fptr(fop);
  242.     uint avail = sbufavailable(s);
  243.     int status;
  244.  
  245.     if (s->end_status == EOFC) {
  246.     /* More data needed, but none available, so this is a syntax error. */
  247.     return_error(e_syntaxerror);
  248.     }
  249.     status = s_process_read_buf(s);
  250.     if (sbufavailable(s) > avail)
  251.     return 0;
  252.     if (status == 0)
  253.     status = s->end_status;
  254.     switch (status) {
  255.     case EOFC:
  256.         /* We just discovered that we're at EOF. */
  257.         /* Let the caller find this out. */
  258.         return 0;
  259.     case ERRC:
  260.         return_error(e_ioerror);
  261.     case INTC:
  262.     case CALLC:
  263.         {
  264.         ref rstate[2];
  265.         scanner_state *pstate;
  266.         int nstate = (push_file ? 2 : 1);
  267.  
  268.         if (save) {
  269.             pstate =
  270.             ialloc_struct(scanner_state, &st_scanner_state,
  271.                       "scan_handle_refill");
  272.             if (pstate == 0)
  273.             return_error(e_VMerror);
  274.             *pstate = *sstate;
  275.         } else
  276.             pstate = sstate;
  277.         /* If push_file is true, we want to push the file on the */
  278.         /* o-stack before the state, for the continuation proc. */
  279.         /* Since the refs passed to s_handle_read_exception */
  280.         /* are pushed on the e-stack, we must ensure they are */
  281.         /* literal, and also pass them in the opposite order! */
  282.         make_istruct(&rstate[0], 0, pstate);
  283.         rstate[1] = *fop;
  284.         r_clear_attrs(&rstate[1], a_executable);
  285.         return s_handle_read_exception(i_ctx_p, status, fop,
  286.                            rstate, nstate, cont);
  287.         }
  288.     }
  289.     /* No more data available, but no exception.  How can this be? */
  290.     lprintf("Can't refill scanner input buffer!");
  291.     return_error(e_Fatal);
  292. }
  293.  
  294. /*
  295.  * Handle a comment.  The 'saved' argument is needed only for
  296.  * tracing printout.
  297.  */
  298. private int
  299. scan_comment(i_ctx_t *i_ctx_p, ref *pref, scanner_state *pstate,
  300.          const byte * base, const byte * end, bool saved)
  301. {
  302.     uint len = (uint) (end - base);
  303.     int code;
  304. #ifdef DEBUG
  305.     const char *sstr = (saved ? ">" : "");
  306. #endif
  307.  
  308.     if (len > 1 && (base[1] == '%' || base[1] == '!')) {
  309.     /* Process as a DSC comment if requested. */
  310. #ifdef DEBUG
  311.     if (gs_debug_c('%')) {
  312.         dlprintf2("[%%%%%s%c]", sstr, (len >= 3 ? '+' : '-'));
  313.         fwrite(base, 1, len, dstderr);
  314.         dputs("\n");
  315.     }
  316. #endif
  317.     if (scan_dsc_proc != NULL) {
  318.         code = scan_dsc_proc(base, len);
  319.         return (code < 0 ? code : 0);
  320.     }
  321.     if (pstate->s_options & SCAN_PROCESS_DSC_COMMENTS) {
  322.         code = scan_DSC_Comment;
  323.         goto comment;
  324.     }
  325.     /* Treat as an ordinary comment. */
  326.     }
  327. #ifdef DEBUG
  328.     else {
  329.     if (gs_debug_c('%')) {
  330.         dlprintf2("[%% %s%c]", sstr, (len >= 2 ? '+' : '-'));
  331.         fwrite(base, 1, len, dstderr);
  332.         dputs("\n");
  333.     }
  334.     }
  335. #endif
  336.     if (scan_comment_proc != NULL) {
  337.     code = scan_comment_proc(base, len);
  338.     return (code < 0 ? code : 0);
  339.     }
  340.     if (pstate->s_options & SCAN_PROCESS_COMMENTS) {
  341.     code = scan_Comment;
  342.     goto comment;
  343.     }
  344.     return 0;
  345.  comment:
  346.     {
  347.     byte *cstr = ialloc_string(len, "scan_comment");
  348.  
  349.     if (cstr == 0)
  350.         return_error(e_VMerror);
  351.     memcpy(cstr, base, len);
  352.     make_string(pref, a_all | icurrent_space, len, cstr);
  353.     }
  354.     return code;
  355. }
  356.  
  357. /* Read a token from a string. */
  358. /* Update the string if succesful. */
  359. int
  360. scan_string_token_options(i_ctx_t *i_ctx_p, ref * pstr, ref * pref,
  361.               int options)
  362. {
  363.     stream st;
  364.     stream *s = &st;
  365.     scanner_state state;
  366.     int code;
  367.  
  368.     if (!r_has_attr(pstr, a_read))
  369.     return_error(e_invalidaccess);
  370.     sread_string(s, pstr->value.bytes, r_size(pstr));
  371.     scanner_state_init_options(&state, options | SCAN_FROM_STRING);
  372.     switch (code = scan_token(i_ctx_p, s, pref, &state)) {
  373.     default:        /* error or comment */
  374.         if (code < 0)
  375.         break;
  376.         /* falls through */
  377.     case 0:        /* read a token */
  378.     case scan_BOS:
  379.         {
  380.         uint pos = stell(s);
  381.  
  382.         pstr->value.bytes += pos;
  383.         r_dec_size(pstr, pos);
  384.         }
  385.         break;
  386.     case scan_Refill:    /* error */
  387.         code = gs_note_error(e_syntaxerror);
  388.     case scan_EOF:
  389.         break;
  390.     }
  391.     return code;
  392. }
  393.  
  394. /*
  395.  * Read a token from a stream.  Return 0 if an ordinary token was read,
  396.  * >0 for special situations (see iscan.h).
  397.  * If the token required a terminating character (i.e., was a name or
  398.  * number) and the next character was whitespace, read and discard
  399.  * that character.  Note that the state is relevant for e_VMerror
  400.  * as well as for scan_Refill.
  401.  */
  402. int
  403. scan_token(i_ctx_t *i_ctx_p, stream * s, ref * pref, scanner_state * pstate)
  404. {
  405.     ref *myref = pref;
  406.     int retcode = 0;
  407.     int c;
  408.  
  409.     s_declare_inline(s, sptr, endptr);
  410. #define scan_begin_inline() s_begin_inline(s, sptr, endptr)
  411. #define scan_getc() sgetc_inline(s, sptr, endptr)
  412. #define scan_putback() sputback_inline(s, sptr, endptr)
  413. #define scan_end_inline() s_end_inline(s, sptr, endptr)
  414.     const byte *newptr;
  415.     byte *daptr;
  416.  
  417. #define sreturn(code)\
  418.   { retcode = gs_note_error(code); goto sret; }
  419. #define sreturn_no_error(code)\
  420.   { scan_end_inline(); return(code); }
  421. #define if_not_spush1()\
  422.   if ( osp < ostop ) osp++;\
  423.   else if ( (retcode = ref_stack_push(&o_stack, 1)) >= 0 )\
  424.     ;\
  425.   else
  426. #define spop1()\
  427.   if ( osp >= osbot ) osp--;\
  428.   else ref_stack_pop(&o_stack, 1)
  429.     int max_name_ctype =
  430.     (recognize_btokens()? ctype_name : ctype_btoken);
  431.  
  432. #define scan_sign(sign, ptr)\
  433.   switch ( *ptr ) {\
  434.     case '-': sign = -1; ptr++; break;\
  435.     case '+': sign = 1; ptr++; break;\
  436.     default: sign = 0;\
  437.   }
  438. #define ensure2_back(styp,nback)\
  439.   if ( sptr >= endptr ) { sptr -= nback; scan_type = styp; goto pause; }
  440. #define ensure2(styp) ensure2_back(styp, 1)
  441.     byte s1[2];
  442.     const byte *const decoder = scan_char_decoder;
  443.     int status;
  444.     int sign;
  445.     const bool check_only = (pstate->s_options & SCAN_CHECK_ONLY) != 0;
  446.     scanner_state sstate;
  447.  
  448. #define pstack sstate.s_pstack
  449. #define pdepth sstate.s_pdepth
  450. #define scan_type sstate.s_scan_type
  451. #define da sstate.s_da
  452. #define name_type sstate.s_ss.s_name.s_name_type
  453. #define try_number sstate.s_ss.s_name.s_try_number
  454.  
  455.     if (pstate->s_pstack != 0) {
  456.     if_not_spush1()
  457.         return retcode;
  458.     myref = osp;
  459.     }
  460.     /* Check whether we are resuming after an interruption. */
  461.     if (pstate->s_scan_type != scanning_none) {
  462.     sstate = *pstate;
  463.     if (!da.is_dynamic && da.base != da.buf) {
  464.         /* The da contains some self-referencing pointers. */
  465.         /* Fix them up now. */
  466.         uint next = da.next - da.base;
  467.         uint limit = da.limit - da.base;
  468.  
  469.         da.base = da.buf;
  470.         da.next = da.buf + next;
  471.         da.limit = da.buf + limit;
  472.     }
  473.     daptr = da.next;
  474.     switch (scan_type) {
  475.         case scanning_binary:
  476.         retcode = (*sstate.s_ss.binary.cont)
  477.             (i_ctx_p, s, myref, &sstate);
  478.         scan_begin_inline();
  479.         if (retcode == scan_Refill)
  480.             goto pause;
  481.         goto sret;
  482.         case scanning_comment:
  483.         scan_begin_inline();
  484.         goto cont_comment;
  485.         case scanning_name:
  486.         goto cont_name;
  487.         case scanning_string:
  488.         goto cont_string;
  489.         default:
  490.         return_error(e_Fatal);
  491.     }
  492.     }
  493.     /* Fetch any state variables that are relevant even if */
  494.     /* scan_type == scanning_none. */
  495.     pstack = pstate->s_pstack;
  496.     pdepth = pstate->s_pdepth;
  497.     sstate.s_options = pstate->s_options;
  498.     scan_begin_inline();
  499.     /*
  500.      * Loop invariants:
  501.      *      If pstack != 0, myref = osp, and *osp is a valid slot.
  502.      */
  503.   top:c = scan_getc();
  504.     if_debug1('S', (c >= 32 && c <= 126 ? "`%c'" : c >= 0 ? "`\\%03o'" : "`%d'"), c);
  505.     switch (c) {
  506.     case ' ':
  507.     case '\f':
  508.     case '\t':
  509.     case char_CR:
  510.     case char_EOL:
  511.     case char_NULL:
  512.         goto top;
  513.     case '[':
  514.     case ']':
  515.         s1[0] = (byte) c;
  516.         retcode = name_ref(s1, 1, myref, 1);    /* can't fail */
  517.         r_set_attrs(myref, a_executable);
  518.         break;
  519.     case '<':
  520.         if (scan_enable_level2) {
  521.         ensure2(scanning_none);
  522.         c = scan_getc();
  523.         switch (c) {
  524.             case '<':
  525.             scan_putback();
  526.             name_type = 0;
  527.             try_number = false;
  528.             goto try_funny_name;
  529.             case '~':
  530.             s_A85D_init_inline(&sstate.s_ss.a85d);
  531.             sstate.s_ss.st.template = &s_A85D_template;
  532.             goto str;
  533.         }
  534.         scan_putback();
  535.         }
  536.         s_AXD_init_inline(&sstate.s_ss.axd);
  537.         sstate.s_ss.st.template = &s_AXD_template;
  538.       str:scan_end_inline();
  539.         dynamic_init(&da, imemory);
  540.       cont_string:for (;;) {
  541.         stream_cursor_write w;
  542.  
  543.         w.ptr = da.next - 1;
  544.         w.limit = da.limit - 1;
  545.         status = (*sstate.s_ss.st.template->process)
  546.             (&sstate.s_ss.st, &s->cursor.r, &w,
  547.              s->end_status == EOFC);
  548.         if (!check_only)
  549.             da.next = w.ptr + 1;
  550.         switch (status) {
  551.             case 0:
  552.             status = s->end_status;
  553.             if (status < 0) {
  554.                 if (status == EOFC) {
  555.                 if (check_only) {
  556.                     retcode = scan_Refill;
  557.                     scan_type = scanning_string;
  558.                     goto suspend;
  559.                 } else
  560.                     sreturn(e_syntaxerror);
  561.                 }
  562.                 break;
  563.             }
  564.             s_process_read_buf(s);
  565.             continue;
  566.             case 1:
  567.             if (!check_only) {
  568.                 retcode = dynamic_grow(&da, da.next, max_string_size);
  569.                 if (retcode == e_VMerror) {
  570.                 scan_type = scanning_string;
  571.                 goto suspend;
  572.                 } else if (retcode < 0)
  573.                 sreturn(retcode);
  574.             }
  575.             continue;
  576.         }
  577.         break;
  578.         }
  579.         scan_begin_inline();
  580.         switch (status) {
  581.         default:
  582.             /*case ERRC: */
  583.             sreturn(e_syntaxerror);
  584.         case INTC:
  585.         case CALLC:
  586.             scan_type = scanning_string;
  587.             goto pause;
  588.         case EOFC:
  589.             ;
  590.         }
  591.         retcode = dynamic_make_string(i_ctx_p, myref, &da, da.next);
  592.         if (retcode < 0) {    /* VMerror */
  593.         sputback(s);    /* rescan ) */
  594.         scan_type = scanning_string;
  595.         goto suspend;
  596.         }
  597.         break;
  598.     case '(':
  599.         sstate.s_ss.pssd.from_string =
  600.         ((pstate->s_options & SCAN_FROM_STRING) != 0) &&
  601.         !scan_enable_level2;
  602.         s_PSSD_init_inline(&sstate.s_ss.pssd);
  603.         sstate.s_ss.st.template = &s_PSSD_template;
  604.         goto str;
  605.     case '{':
  606.         if (pstack == 0) {    /* outermost procedure */
  607.         if_not_spush1() {
  608.             scan_putback();
  609.             scan_type = scanning_none;
  610.             goto pause_ret;
  611.         }
  612.         pdepth = ref_stack_count_inline(&o_stack);
  613.         }
  614.         make_int(osp, pstack);
  615.         pstack = ref_stack_count_inline(&o_stack);
  616.         if_debug3('S', "[S{]d=%d, s=%d->%d\n",
  617.               pdepth, (int)osp->value.intval, pstack);
  618.         goto snext;
  619.     case '>':
  620.         if (scan_enable_level2) {
  621.         ensure2(scanning_none);
  622.         name_type = 0;
  623.         try_number = false;
  624.         goto try_funny_name;
  625.         }
  626.         /* falls through */
  627.     case ')':
  628.         sreturn(e_syntaxerror);
  629.     case '}':
  630.         if (pstack == 0)
  631.         sreturn(e_syntaxerror);
  632.         osp--;
  633.         {
  634.         uint size = ref_stack_count_inline(&o_stack) - pstack;
  635.         ref arr;
  636.  
  637.         if_debug4('S', "[S}]d=%d, s=%d->%ld, c=%d\n",
  638.               pdepth, pstack,
  639.               (pstack == pdepth ? 0 :
  640.                ref_stack_index(&o_stack, size)->value.intval),
  641.               size + pstack);
  642.         myref = (pstack == pdepth ? pref : &arr);
  643.         if (check_only) {
  644.             make_empty_array(myref, 0);
  645.             ref_stack_pop(&o_stack, size);
  646.         } else if (ref_array_packing.value.boolval) {
  647.             retcode = make_packed_array(myref, &o_stack, size,
  648.                         idmemory, "scanner(packed)");
  649.             if (retcode < 0) {    /* must be VMerror */
  650.             osp++;
  651.             scan_putback();
  652.             scan_type = scanning_none;
  653.             goto pause_ret;
  654.             }
  655.             r_set_attrs(myref, a_executable);
  656.         } else {
  657.             retcode = ialloc_ref_array(myref,
  658.                            a_executable + a_all, size,
  659.                            "scanner(proc)");
  660.             if (retcode < 0) {    /* must be VMerror */
  661.             osp++;
  662.             scan_putback();
  663.             scan_type = scanning_none;
  664.             goto pause_ret;
  665.             }
  666.             retcode = ref_stack_store(&o_stack, myref, size, 0, 1,
  667.                           false, idmemory, "scanner");
  668.             if (retcode < 0) {
  669.             ifree_ref_array(myref, "scanner(proc)");
  670.             sreturn(retcode);
  671.             }
  672.             ref_stack_pop(&o_stack, size);
  673.         }
  674.         if (pstack == pdepth) {        /* This was the top-level procedure. */
  675.             spop1();
  676.             pstack = 0;
  677.         } else {
  678.             if (osp < osbot)
  679.             ref_stack_pop_block(&o_stack);
  680.             pstack = osp->value.intval;
  681.             *osp = arr;
  682.             goto snext;
  683.         }
  684.         }
  685.         break;
  686.     case '/':
  687.         ensure2(scanning_none);
  688.         c = scan_getc();
  689.         if (c == '/') {
  690.         name_type = 2;
  691.         c = scan_getc();
  692.         } else
  693.         name_type = 1;
  694.         try_number = false;
  695.         switch (decoder[c]) {
  696.         case ctype_name:
  697.         default:
  698.             goto do_name;
  699.         case ctype_btoken:
  700.             if (!recognize_btokens())
  701.             goto do_name;
  702.             /* otherwise, an empty name */
  703.         case ctype_exception:
  704.         case ctype_space:
  705.             /*
  706.              * Amazingly enough, the Adobe implementations don't accept
  707.              * / or // followed by [, ], <<, or >>, so we do the same.
  708.              * (Older versions of our code had a ctype_other case here
  709.              * that handled these specially.)
  710.              */
  711.         case ctype_other:
  712.             da.base = da.limit = daptr = 0;
  713.             da.is_dynamic = false;
  714.             goto nx;
  715.         }
  716.     case '%':
  717.         {            /* Scan as much as possible within the buffer. */
  718.         const byte *base = sptr;
  719.         const byte *end;
  720.  
  721.         while (++sptr < endptr)        /* stop 1 char early */
  722.             switch (*sptr) {
  723.             case char_CR:
  724.                 end = sptr;
  725.                 if (sptr[1] == char_EOL)
  726.                 sptr++;
  727.               cend:    /* Check for externally processed comments. */
  728.                 retcode = scan_comment(i_ctx_p, myref, &sstate,
  729.                            base, end, false);
  730.                 if (retcode != 0)
  731.                 goto comment;
  732.                 goto top;
  733.             case char_EOL:
  734.             case '\f':
  735.                 end = sptr;
  736.                 goto cend;
  737.             }
  738.         /*
  739.          * We got to the end of the buffer while inside a comment.
  740.          * If there is a possibility that we must pass the comment
  741.          * to an external procedure, move what we have collected
  742.          * so far into a private buffer now.
  743.          */
  744. #define comment_line da.buf
  745.         --sptr;
  746.         comment_line[1] = 0;
  747.         if (scan_comment_proc != NULL ||
  748.             ((sptr == base || base[1] == '%') &&
  749.              scan_dsc_proc != NULL)
  750.             ) {        /* Could be an externally processable comment. */
  751.             uint len = sptr + 1 - base;
  752.  
  753.             memcpy(comment_line, base, len);
  754.             daptr = comment_line + len;
  755.         } else {    /* Not a DSC comment. */
  756.             daptr = comment_line + (max_comment_line + 1);
  757.         }
  758.         da.base = comment_line;
  759.         da.is_dynamic = false;
  760.         }
  761.         /* Enter here to continue scanning a comment. */
  762.         /* daptr must be set. */
  763.       cont_comment:for (;;) {
  764.         switch ((c = scan_getc())) {
  765.             default:
  766.             if (c < 0)
  767.                 switch (c) {
  768.                 case INTC:
  769.                 case CALLC:
  770.                     da.next = daptr;
  771.                     scan_type = scanning_comment;
  772.                     goto pause;
  773.                 case EOFC:
  774.                     /*
  775.                      * One would think that an EOF in a comment
  776.                      * should be a syntax error, but there are
  777.                      * quite a number of files that end that way.
  778.                      */
  779.                     goto end_comment;
  780.                 default:
  781.                     sreturn(e_syntaxerror);
  782.                 }
  783.             if (daptr < comment_line + max_comment_line)
  784.                 *daptr++ = c;
  785.             continue;
  786.             case char_CR:
  787.             case char_EOL:
  788.             case '\f':
  789.               end_comment:
  790.             retcode = scan_comment(i_ctx_p, pref, &sstate,
  791.                            comment_line, daptr, true);
  792.             if (retcode != 0)
  793.                 goto comment;
  794.             goto top;
  795.         }
  796.         }
  797. #undef comment_line
  798.         /*NOTREACHED */
  799.     case EOFC:
  800.         if (pstack != 0) {
  801.         if (check_only)
  802.             goto pause;
  803.         sreturn(e_syntaxerror);
  804.         }
  805.         retcode = scan_EOF;
  806.         break;
  807.     case ERRC:
  808.         sreturn(e_ioerror);
  809.  
  810.         /* Check for a Level 2 funny name (<< or >>). */
  811.         /* c is '<' or '>'.  We already did an ensure2. */
  812.       try_funny_name:
  813.         {
  814.         int c1 = scan_getc();
  815.  
  816.         if (c1 == c) {
  817.             s1[0] = s1[1] = c;
  818.             name_ref(s1, 2, myref, 1);    /* can't fail */
  819.             goto have_name;
  820.         }
  821.         scan_putback();
  822.         }
  823.         sreturn(e_syntaxerror);
  824.  
  825.         /* Handle separately the names that might be a number. */
  826.     case '0':
  827.     case '1':
  828.     case '2':
  829.     case '3':
  830.     case '4':
  831.     case '5':
  832.     case '6':
  833.     case '7':
  834.     case '8':
  835.     case '9':
  836.     case '.':
  837.         sign = 0;
  838.     nr:        /*
  839.          * Skip a leading sign, if any, by conditionally passing
  840.          * sptr + 1 rather than sptr.  Also, if the last character
  841.          * in the buffer is a CR, we must stop the scan 1 character
  842.          * early, to be sure that we can test for CR+LF within the
  843.          * buffer, by passing endptr rather than endptr + 1.
  844.          */
  845.         retcode = scan_number(sptr + (sign & 1),
  846.             endptr /*(*endptr == char_CR ? endptr : endptr + 1) */ ,
  847.                   sign, myref, &newptr);
  848.         if (retcode == 1 && decoder[newptr[-1]] == ctype_space) {
  849.         sptr = newptr - 1;
  850.         if (*sptr == char_CR && sptr[1] == char_EOL)
  851.             sptr++;
  852.         retcode = 0;
  853.         ref_mark_new(myref);
  854.         break;
  855.         }
  856.         name_type = 0;
  857.         try_number = true;
  858.         goto do_name;
  859.     case '+':
  860.         sign = 1;
  861.         goto nr;
  862.     case '-':
  863.         sign = -1;
  864.         goto nr;
  865.  
  866.         /* Check for a binary object */
  867. #define case4(c) case c: case c+1: case c+2: case c+3
  868.       case4(128): case4(132): case4(136): case4(140):
  869.       case4(144): case4(148): case4(152): case4(156):
  870. #undef case4
  871.         if (recognize_btokens()) {
  872.         scan_end_inline();
  873.         retcode = scan_binary_token(i_ctx_p, s, myref, &sstate);
  874.         scan_begin_inline();
  875.         if (retcode == scan_Refill)
  876.             goto pause;
  877.         break;
  878.         }
  879.         /* Not a binary object, fall through. */
  880.  
  881.         /* The default is a name. */
  882.     default:
  883.         if (c < 0) {
  884.         dynamic_init(&da, name_memory());    /* da state must be clean */
  885.         scan_type = scanning_none;
  886.         goto pause;
  887.         }
  888.         /* Populate the switch with enough cases to force */
  889.         /* simple compilers to use a dispatch rather than tests. */
  890.     case '!':
  891.     case '"':
  892.     case '#':
  893.     case '$':
  894.     case '&':
  895.     case '\'':
  896.     case '*':
  897.     case ',':
  898.     case '=':
  899.     case ':':
  900.     case ';':
  901.     case '?':
  902.     case '@':
  903.     case 'A':
  904.     case 'B':
  905.     case 'C':
  906.     case 'D':
  907.     case 'E':
  908.     case 'F':
  909.     case 'G':
  910.     case 'H':
  911.     case 'I':
  912.     case 'J':
  913.     case 'K':
  914.     case 'L':
  915.     case 'M':
  916.     case 'N':
  917.     case 'O':
  918.     case 'P':
  919.     case 'Q':
  920.     case 'R':
  921.     case 'S':
  922.     case 'T':
  923.     case 'U':
  924.     case 'V':
  925.     case 'W':
  926.     case 'X':
  927.     case 'Y':
  928.     case 'Z':
  929.     case '\\':
  930.     case '^':
  931.     case '_':
  932.     case '`':
  933.     case 'a':
  934.     case 'b':
  935.     case 'c':
  936.     case 'd':
  937.     case 'e':
  938.     case 'f':
  939.     case 'g':
  940.     case 'h':
  941.     case 'i':
  942.     case 'j':
  943.     case 'k':
  944.     case 'l':
  945.     case 'm':
  946.     case 'n':
  947.     case 'o':
  948.     case 'p':
  949.     case 'q':
  950.     case 'r':
  951.     case 's':
  952.     case 't':
  953.     case 'u':
  954.     case 'v':
  955.     case 'w':
  956.     case 'x':
  957.     case 'y':
  958.     case 'z':
  959.     case '|':
  960.     case '~':
  961.         /* Common code for scanning a name. */
  962.         /* try_number and name_type are already set. */
  963.         /* We know c has ctype_name (or maybe ctype_btoken) */
  964.         /* or is a digit. */
  965.         name_type = 0;
  966.         try_number = false;
  967.       do_name:
  968.         /* Try to scan entirely within the stream buffer. */
  969.         /* We stop 1 character early, so we don't switch buffers */
  970.         /* looking ahead if the name is terminated by \r\n. */
  971.         da.base = (byte *) sptr;
  972.         da.is_dynamic = false;
  973.         {
  974.         const byte *endp1 = endptr - 1;
  975.  
  976.         do {
  977.             if (sptr >= endp1)    /* stop 1 early! */
  978.             goto dyn_name;
  979.         }
  980.         while (decoder[*++sptr] <= max_name_ctype);    /* digit or name */
  981.         }
  982.         /* Name ended within the buffer. */
  983.         daptr = (byte *) sptr;
  984.         c = *sptr;
  985.         goto nx;
  986.       dyn_name:        /* Name extended past end of buffer. */
  987.         scan_end_inline();
  988.         /* Initialize the dynamic area. */
  989.         /* We have to do this before the next */
  990.         /* sgetc, which will overwrite the buffer. */
  991.         da.limit = (byte *)++ sptr;
  992.         da.memory = name_memory();
  993.         retcode = dynamic_grow(&da, da.limit, name_max_string);
  994.         if (retcode < 0) {
  995.         dynamic_save(&da);
  996.         if (retcode != e_VMerror)
  997.             sreturn(retcode);
  998.         scan_type = scanning_name;
  999.         goto pause_ret;
  1000.         }
  1001.         daptr = da.next;
  1002.         /* Enter here to continue scanning a name. */
  1003.         /* daptr must be set. */
  1004.       cont_name:scan_begin_inline();
  1005.         while (decoder[c = scan_getc()] <= max_name_ctype) {
  1006.         if (daptr == da.limit) {
  1007.             retcode = dynamic_grow(&da, daptr,
  1008.                        name_max_string);
  1009.             if (retcode < 0) {
  1010.             dynamic_save(&da);
  1011.             if (retcode != e_VMerror)
  1012.                 sreturn(retcode);
  1013.             scan_putback();
  1014.             scan_type = scanning_name;
  1015.             goto pause_ret;
  1016.             }
  1017.             daptr = da.next;
  1018.         }
  1019.         *daptr++ = c;
  1020.         }
  1021.       nx:switch (decoder[c]) {
  1022.         case ctype_btoken:
  1023.         case ctype_other:
  1024.             scan_putback();
  1025.             break;
  1026.         case ctype_space:
  1027.             /* Check for \r\n */
  1028.             if (c == char_CR) {
  1029.             if (sptr >= endptr) {    /* ensure2 *//* We have to check specially for */
  1030.                 /* the case where the very last */
  1031.                 /* character of a file is a CR. */
  1032.                 if (s->end_status != EOFC) {
  1033.                 sptr--;
  1034.                 goto pause_name;
  1035.                 }
  1036.             } else if (sptr[1] == char_EOL)
  1037.                 sptr++;
  1038.             }
  1039.             break;
  1040.         case ctype_exception:
  1041.             switch (c) {
  1042.             case INTC:
  1043.             case CALLC:
  1044.                 goto pause_name;
  1045.             case ERRC:
  1046.                 sreturn(e_ioerror);
  1047.             case EOFC:
  1048.                 break;
  1049.             }
  1050.         }
  1051.         /* Check for a number */
  1052.         if (try_number) {
  1053.         const byte *base = da.base;
  1054.  
  1055.         scan_sign(sign, base);
  1056.         retcode = scan_number(base, daptr, sign, myref, &newptr);
  1057.         if (retcode == 1) {
  1058.             ref_mark_new(myref);
  1059.             retcode = 0;
  1060.         } else if (retcode != e_syntaxerror) {
  1061.             dynamic_free(&da);
  1062.             if (name_type == 2)
  1063.             sreturn(e_syntaxerror);
  1064.             break;    /* might be e_limitcheck */
  1065.         }
  1066.         }
  1067.         if (da.is_dynamic) {    /* We've already allocated the string on the heap. */
  1068.         uint size = daptr - da.base;
  1069.  
  1070.         retcode = name_ref(da.base, size, myref, -1);
  1071.         if (retcode >= 0) {
  1072.             dynamic_free(&da);
  1073.         } else {
  1074.             retcode = dynamic_resize(&da, size);
  1075.             if (retcode < 0) {    /* VMerror */
  1076.             if (c != EOFC)
  1077.                 scan_putback();
  1078.             scan_type = scanning_name;
  1079.             goto pause_ret;
  1080.             }
  1081.             retcode = name_ref(da.base, size, myref, 2);
  1082.         }
  1083.         } else {
  1084.         retcode = name_ref(da.base, (uint) (daptr - da.base),
  1085.                    myref, !s->foreign);
  1086.         }
  1087.         /* Done scanning.  Check for preceding /'s. */
  1088.         if (retcode < 0) {
  1089.         if (retcode != e_VMerror)
  1090.             sreturn(retcode);
  1091.         if (!da.is_dynamic) {
  1092.             da.next = daptr;
  1093.             dynamic_save(&da);
  1094.         }
  1095.         if (c != EOFC)
  1096.             scan_putback();
  1097.         scan_type = scanning_name;
  1098.         goto pause_ret;
  1099.         }
  1100.       have_name:switch (name_type) {
  1101.         case 0:    /* ordinary executable name */
  1102.             if (r_has_type(myref, t_name))    /* i.e., not a number */
  1103.             r_set_attrs(myref, a_executable);
  1104.         case 1:    /* quoted name */
  1105.             break;
  1106.         case 2:    /* immediate lookup */
  1107.             {
  1108.             ref *pvalue;
  1109.  
  1110.             if (!r_has_type(myref, t_name))
  1111.                 sreturn(e_undefined);
  1112.             if ((pvalue = dict_find_name(myref)) == 0)
  1113.                 sreturn(e_undefined);
  1114.             if (pstack != 0 &&
  1115.                 r_space(pvalue) > ialloc_space(idmemory)
  1116.                 )
  1117.                 sreturn(e_invalidaccess);
  1118.             ref_assign_new(myref, pvalue);
  1119.             }
  1120.         }
  1121.     }
  1122.   sret:if (retcode < 0) {
  1123.     scan_end_inline();
  1124.     if (pstack != 0)
  1125.         ref_stack_pop(&o_stack,
  1126.               ref_stack_count(&o_stack) - (pdepth - 1));
  1127.     return retcode;
  1128.     }
  1129.     /* If we are at the top level, return the object, */
  1130.     /* otherwise keep going. */
  1131.     if (pstack == 0) {
  1132.     scan_end_inline();
  1133.     return retcode;
  1134.     }
  1135.   snext:if_not_spush1() {
  1136.     scan_end_inline();
  1137.     scan_type = scanning_none;
  1138.     goto save;
  1139.     }
  1140.     myref = osp;
  1141.     goto top;
  1142.  
  1143.     /* Pause for an interrupt or callout. */
  1144.   pause_name:
  1145.     /* If we're still scanning within the stream buffer, */
  1146.     /* move the characters to the private buffer (da.buf) now. */
  1147.     da.next = daptr;
  1148.     dynamic_save(&da);
  1149.     scan_type = scanning_name;
  1150.   pause:
  1151.     retcode = scan_Refill;
  1152.   pause_ret:
  1153.     scan_end_inline();
  1154.   suspend:
  1155.     if (pstack != 0)
  1156.     osp--;            /* myref */
  1157.   save:
  1158.     *pstate = sstate;
  1159.     return retcode;
  1160.  
  1161.     /* Handle a scanned comment. */
  1162.  comment:
  1163.     if (retcode < 0)
  1164.     goto sret;
  1165.     scan_end_inline();
  1166.     scan_type = scanning_none;
  1167.     goto save;
  1168. }
  1169.